//
//  KYFeedBackController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/25.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYFeedBackController.h"
#import <MessageUI/MessageUI.h>

@interface KYFeedBackController ()<MFMailComposeViewControllerDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation KYFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 0.强引用我们的webView不能创建完了就销毁
    self.webView = [[UIWebView alloc] init];
    
    // 1.添加分组
    [self setUpGroup0];
    [self setUpGroup1];
}


- (void)setUpGroup0
{
    __weak typeof(self) selfVc = self;
    
    KYSettingGroup *group = [self addGroup];
    
    KYSettingArrowItem *userRate = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"用户评分", nil)];
    userRate.option = ^{  // 跳转到App Store (发布过的程序都会有一个appid) 固定写法
        NSString *appid = @"1237029414";
        NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
        NSURL *url = [NSURL URLWithString:str];
        [[UIApplication sharedApplication] openURL:url];
    };
    
    KYSettingArrowItem *msgContact = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"邮件反馈", nil)];
    msgContact.option = ^{  // 发送邮件
        // 不能发邮件
        if (![MFMailComposeViewController canSendMail]) {
            UIAlertView *mailAlartView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"没有注册手机邮箱", nil) message:NSLocalizedString(@"请到iCloud设置邮箱", nil) delegate:selfVc cancelButtonTitle:NSLocalizedString(@"确定", nil) otherButtonTitles:nil, nil];
            [mailAlartView show];
            
        } else {
            MFMailComposeViewController *msgVc = [[MFMailComposeViewController alloc] init];
            
            // 设置邮件主题
            [msgVc setSubject:NSLocalizedString(@"意见反馈", nil)];
            // 设置邮件内容
            [msgVc setMessageBody:@"我对多啦摄影的的评价" isHTML:NO];
            // 设置收件人列表
            [msgVc setToRecipients:@[@"934830075@qq.com"]];
            // 设置密送人列表
            [msgVc setBccRecipients:@[@"815958137@qq.com"]];
            // 设置代理
            msgVc.mailComposeDelegate = selfVc;
            
            // 显示控制器
            [selfVc presentViewController:msgVc animated:YES completion:nil];
        }
    };
    group.items = @[userRate,msgContact];
}

- (void)setUpGroup1
{
    __weak typeof(self) selfVc = self;
    
    KYSettingGroup *group = [self addGroup];
    KYSettingArrowItem *telContact = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"客服电话", nil)];
    telContact.detailLabel = NSLocalizedString(@"086-15827034256", nil);
    telContact.option = ^{  // 打电话的方式很多种\这种打完了会回到我们的应用程序\telprompt(私有的所以尽量避免用)
        NSURL *url = [NSURL URLWithString:@"tel://15827034256"];
        [selfVc.webView loadRequest:[NSURLRequest requestWithURL:url]];
    };
    
    group.items = @[telContact];
}

#pragma mark - mail的代理方法
/**
 *  点击取消按钮会自动调用
 */
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIAlertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != 0) return;
    
    // 如果要做事情再这里添加
}

@end
